package com.rqf.ls.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.ls.service.ICommonDataService;
import com.server.database.SQLCreator;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("CommonDataService")
public class CommonDataService implements ICommonDataService {
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 公共方法获取列表
	 * @param table
	 * @param _whereStr
	 * @param sortStr
	 * @param requestInput
	 * @return
	 * @author ServerZhang
	 * @date 2016年5月13日
	 */
	@Override
	public DataVector<Map<String, Object>> getListData(String table,
			String _whereStr, String sortStr, RequestInput requestInput){
		StringBuffer whereStr = new StringBuffer();
		if (!StringUtils.isEmpty(_whereStr)) {
			whereStr.append(_whereStr);
		}
		Integer pageIndex = 0;
		Integer pageSize = Integer.MAX_VALUE;
		if(requestInput != null){
			if (requestInput.getWhereStr() != null
					&& requestInput.getWhereStr().length() > 0) {
				if(!StringUtils.isEmpty(_whereStr)){
					whereStr.append(" AND ");
				}
				whereStr.append("(").append(requestInput.getWhereStr()).append(")");
			}
			if (requestInput.getSortStr() != null
					&& requestInput.getSortStr().length() > 0) {
				sortStr = requestInput.getSortStr();
			}
			pageIndex = requestInput.getPageIndex();
			pageSize = requestInput.getPageSize();
		}
		String sqlCount = SQLCreator.getSqlCount(table,
				whereStr.toString(),null,null);
		String sql = SQLCreator.getSql_Oracle(pageIndex, pageSize, table,
				whereStr.toString(), sortStr,null,null);
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		Integer count = jdbcTemplate.queryForObject(sqlCount, Integer.class);
		return new DataVector<Map<String, Object>>(list, count);
	}
	/**
	 * 公共方法获取表单信息
	 * @param tableName
	 * @param fieldName
	 * @param refNo
	 * @param whereStr
	 * @param sortStr
	 * @return
	 * @author ServerZhang
	 * @date 2016年5月13日
	 */
	@Override
	public Map<String, Object> getFormData(String tableName, String fieldName,
			String refNo, String whereStr, String sortStr) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (tableName != null) {
			StringBuffer sqlBuffer = new StringBuffer("Select * from ");
			sqlBuffer.append(tableName).append(
					" where StatId=1 AND RecordValidity=1");
			if (!StringUtils.isEmpty(whereStr)) {
				sqlBuffer.append(" AND ").append(whereStr);
			}
			if (!StringUtils.isEmpty(fieldName) && !StringUtils.isEmpty(refNo)) {
				sqlBuffer.append(" AND ").append(fieldName).append("='")
						.append(refNo).append("'");
			}
			if (!StringUtils.isEmpty(sortStr)) {
				sqlBuffer.append(" Order by ").append(sortStr);
			}
			List<Map<String, Object>> list = jdbcTemplate
					.queryForList(sqlBuffer.toString());
			if (list.size() > 0) {
				map = list.get(0);
			}
		}
		return map;
	}
	/**
	 * 获取某个字段的唯一List
	 * @param tableName
	 * @param fieldStr
	 * @param whereStr
	 * @param sortStr
	 * @return
	 * @author ServerZhang
	 * @date 2016年5月13日
	 */
	@Override
	public List<Map<String, Object>> getFieldList(String tableName,
			String fieldStr, String whereStr, String sortStr) {
		if (!StringUtils.isEmpty(tableName)) {
			StringBuffer whereBuffer = new StringBuffer(fieldStr+" is not null");
			if (!StringUtils.isEmpty(whereStr)) {
				whereBuffer.append(" AND ").append(whereStr);
			}
			StringBuffer sqlBuffer = new StringBuffer("Select distinct ");
			sqlBuffer.append(fieldStr).append(" from ").append(tableName)
					.append(" where ").append(whereBuffer).append(" order by ")
					.append(sortStr);
			List<Map<String, Object>> list = jdbcTemplate
					.queryForList(sqlBuffer.toString());
			return list;
		} else {
			return new ArrayList<Map<String, Object>>();
		}
	}
	@Override
	public String getNameByPhone(String phoneNo, String userType){
		String table = "lawyer".equals(userType) ? "LSUSER" : "NORMALUSER";
		String sql = "select USERNAME from "+table+" where PHONE='"+phoneNo+"' and ABLE=1";
		String userName = jdbcTemplate.queryForObject(sql, String.class);
		return userName;
		
	}
	 

}
